import {BasePage, PartialPage} from "../core/BasePage";
import {MathUtils} from "../../../utils/MathUtils";
import {PageBuilder, pageFunc, PageSettingKey} from "../PageBuilder";
import {StringUtils} from "../../../utils/StringUtils";
import {Constructor} from "../../../utils/BaseContext";
import {DiscoverPage} from "../../discover/DiscoverPage";
import {HomePage} from "../../homepage/HomePagePage";

const ShareTitles = [
	"军训通——军人的基地"
]
export const DefaultShareImage = "cloud://junxuntong-8g88o26yce05bb47.6a75-junxuntong-8g88o26yce05bb47-1310094760/screenshot.png";

type ShareAppParams = {
	from: "menu" | "button", target: any, webViewUrl: string
}
type ShareAppContent = {
	title?: string, path?: string, imageUrl?: string, promise?: Promise<string>
}
type ShareTimelineContent = {
	title?: string, query?: string, imageUrl?: string
}

export class ShareAppPage extends PartialPage {

	public sharePage: Constructor<BasePage>;
	public extra: any = {};

	public onShare?: (ShareParams) => ShareAppContent;

	constructor(sharePage:
								Constructor<BasePage> |
								(() => Constructor<BasePage>) = DefaultSharePage,
							extra = {}) {
		super();
		console.log("sharePage", sharePage);
		if (sharePage[PageSettingKey])
			this.sharePage = sharePage as Constructor<BasePage>;
		else
			this.sharePage = (sharePage as () => Constructor<BasePage>)();
		this.extra = extra;
	}

	protected get title() { return MathUtils.randomPick(ShareTitles) }

	private get path() { return PageBuilder.getPageSetting(this.sharePage); }

	@pageFunc
	protected onShareAppMessage(obj: ShareAppParams) {
		const path = this.path.path + "?" + StringUtils.makeQueryParam(this.extra);
		console.log("onShareAppMessage path", path);
		return {
			title: this.title, path,
			imageUrl: DefaultShareImage,
			...this.onShare?.(obj)
		} as ShareAppContent
	}
}

export class ShareTimelinePage extends PartialPage {

	public query: any = {};
	public onShare?: () => ShareTimelineContent;

	constructor(query?) {
		super();
		this.query = query || {};
	}

	protected get title() { return MathUtils.randomPick(ShareTitles) }

	@pageFunc
	protected onShareTimeline() {
		const query = StringUtils.makeQueryParam(this.query);
		console.log("onShareTimeline query", query);
		return {
			title: this.title, query,
			// imageUrl: DefaultShareImage,
			...this.onShare?.()
		}
	}
}

export const DefaultSharePage = () => HomePage;
